local sk__chongsheng = fk.CreateSkill {

  name = "sk__chongsheng",

  tags = { Skill.Limited, },

}



sk__chongsheng:addEffect(fk.EnterDying, {
  name = "sk__chongsheng",
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(sk__chongsheng.name) and not target.dead and player:usedSkillTimes(sk__chongsheng.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, sk__chongsheng.name, data,
      "#sk__chongsheng-invoke::"..target.id..":"..math.max(player:getMark("sk__yuanhua"), 1))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, {target.id})
    local n = math.max(player:getMark("sk__yuanhua"), 1)
    if n ~= target.maxHp then
      room:changeMaxHp(target, n - target.maxHp)
    end
    if not target.dead and target:isWounded() then
      room:recover{
        who = target,
        num = target:getLostHp(),
        recoverBy = player,
        skillName = sk__chongsheng.name,
      }
    end
    if not target.dead then
      local generals = room:findGenerals(function(g)
        return Fk.generals[g].kingdom == target.kingdom
      end, 3)
      if #generals == 0 then return end
      local result = room:askForCustomDialog(target, sk__chongsheng.name, "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml", {
        generals,
        {"OK"},
        "#sk__chongsheng-choose",
        {"Cancel"},
      })
      local general = ""
      if result == "" then
        return
      else
        local reply = result
        if reply.choice == "OK" then
          general = reply.cards[1]
        else
          return
        end
      end
      table.removeOne(generals, general)
      room:returnToGeneralPile(generals)
      room:changeHero(target, general, false, false, true, false)
    end
  end,
})

return sk__chongsheng